# =Ruby API for Yahoo! LifeType
#
#  See http://tw.developer.yahoo.com/lifestyle_api.html
#  (C) 2008 Hsu-Shih-Chung < CFC >
#  http://blog.pixnet.net/zusocfc
#
# === LICENSE:
#   
#  BSD(-compatible)
#
# === EXAMPLE:
#  See folder named examples.
#
# === HISTORY:
#  - 0.2 2008-04-17: Bug fix(Bad URL)
#  - 0.1 2008-04-16: Initial release.
#
# === BUGS?
#  zusocfc at gmail dot com
#

=begin
API methods:
  Biz.getDetails:
    id => [ "Y", "ID of the biz" ]
  Biz.listReviews
    id => [ "Y", "ID of the biz" ]
    begin => [ "N", "index of beginning, default 1, must be great than 1" ]
    limit => [ "N", "count limit, default 10, must be great than 0" ]
  Biz.addReview
    id => [ "Y", "ID of the biz" ]
    comment => [ "Y", "comment" ]
    # Only return successul or not.
  Biz.listBuzzBizs
    # No need to set the params.
  Biz.search
    bizname => [ "Y", "Biz name" ]
    address => [ "Y", "The address" ]
    page => [ "N", "Pages, default 1" ]
  Class.listClasses
    id => [ "N", "ID of the class (Space ID), if no id param, it will return the root classes" ]
  Class.listBizsInRange
    class => [ "N", "ID of the class(Space ID, defaut 0, root classes)" ]
    lon => [ "N", "Longitude, default: 121.5438" ]
    lat => [ "N", "Latitude, default: 25.0417" ]
  User.listBookmarks
    id => [ "Y", "ID of the yahoo user" ]
  User.listReviews
    id => [ "Y", "ID of the yahoo user" ]
    biz => [ "Y", "ID of the biz" ]
  Bookmark.listBizs
    id => [ "Y", "ID of the bookmark" ]
    user => [ "Y", "ID of the yahoo user" ]
=end

require 'net/http'
include Net

$AUTHOR = "CFC"
$VERSION = "0.1"
$DATE = "2008-04-16 22:22"

module LifeType
  REQUEST_URL = "http://tw.lifestyle.yahooapis.com/v0.1/"
  
  class Biz
    #--
    # => API methods: [ :getDetails, :listReviews, :addReview, :listBuzzBizs, :search ]
    #++
    # APP ID is the only argument.
    def initialize(app_id)
      @app_id = app_id.to_s
    end
    
    def getDetails(id)
      request = "#{REQUEST_URL}Biz.getDetails?appid=#{@app_id}&id=#{id}"
      return get_result(request)
    end
    
    def listReviews(id, _begin=1, limit = 10)
      request = "#{REQUEST_URL}Biz.listReviews?appid=#{@app_id}&id=#{id}&begin=#{_begin}&limit=#{limit}"
      return get_result(request)
    end
#--
#    def addReview(id, comment)
#      request  = "#{REQUEST_URL}Biz.addReview?appid=#{@app_id}&id=#{id}&comment=#{comment}"
#    end
#++
    
    # No argument for this function.
    # Example:
    ## biz = LifeType::Biz.new(APPID)
    ## xml_parse(biz.listBuzzBizs)
    def listBuzzBizs
      request = "#{REQUEST_URL}Biz.listBuzzBizs?appid=#{@app_id}"
      return get_result(request)
    end
    
    def search(bizname, address="", page=1)
      request = "#{REQUEST_URL}Biz.search?appid=#{@app_id}&BizName=#{bizname}&address=#{address}&page=#{page}"
      return get_result(request)
    end
    
  end

  # Please don't do this:
  ## include LifeType
  ## Class.new(APPID) #=> Error!!
  ## LifeType::Class.new(APPID) #=> Success!!
  class Class
    #--
    # => API methods: [ :listClasses, :listBizsInRange ]
    #++
    # APP ID is the only argument.
    def initialize(app_id)
      @app_id = app_id
    end
    
    def listClasses(id = "")
      request = "#{REQUEST_URL}Class.listClasses?appid=#{@app_id}&id=#{id}"
      return get_result(request)
    end
    
    def listBuzzInRange(_class=0, lon=121.5438, lat=25.0417)
      request = "#{REQUEST_URL}Class.listBuzzInRange?appid=#{@app_id}&class=#{_class}&lon=#{lon}&lat=#{lat}"
      return get_result(request)
    end
  end

  class User
    #--
    # => API methods: [ :listBookmarks, :listReviews ]
    #++
    # APP ID is the only argument.
    def initialize(app_id)
      @app_id = app_id
    end
    
    def listBookmarks(id)
      request = "#{REQUEST_URL}User.listBookmarks?appid=#{@app_id}&id=#{id}"
      return get_result(request)
    end
    
    def listReviews(id, biz="")
      request = "#{REQUEST_URL}User.listReviews?appid=#{@app_id}&id=#{id}&biz=#{biz}"
      return get_result(request)
    end
  end

  class Bookmark
    #--
    # => API methods: [ :listBizs ]
    #++
    # APP ID is the only argument.
    def initialize(app_id)
      @app_id = app_id
    end
    
    def listBizs(id, user)
      request = "#{REQUEST_URL}Bookmark.listBizs?appid=#{@app_id}&id=#{id}&user=#{user}"
      return get_result(request)
    end
  end
end

def get_result(request)
	return HTTP.get_response(URI.parse(URI.escape(request))).body
end